/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hanze.shoestring.dao.impl;

import java.util.List;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import nl.hanze.shoestring.dao.ReisDAO;
import nl.hanze.shoestring.domain.*;

/**
 *
 * @author alex
 */
public class ReisDAOImpl extends BaseDAOImpl<Reis> implements ReisDAO {

    public ReisDAOImpl() {
    }
 /**
  * Zoeken op naam van reis, omschrijving van reis, naam van reistype en naam van land
  * @param name
  * @return 
  */
    @Override
    public List<Reis> find(String name) {
        boolean hasName = name != null && name.length() > 0;
        if (!hasName) {
            return getAll();
        }
        
        open();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Reis> query = cb.createQuery(Reis.class);
        Root<Reis> reis = query.from(Reis.class);
        Join<Reis, Reistype> reistype = reis.join(Reis_.reistypeId);
        Join<Reis, Land> land = reis.join(Reis_.landId);
        Predicate p = cb.or(cb.like(reis.get(Reis_.naam), '%' + name + '%'),
                cb.like(reis.get(Reis_.omschrijving),'%' + name + '%'),
                cb.like(reistype.get(Reistype_.naam), '%' + name + '%'),
                cb.like(land.get(Land_.naam), '%' + name + '%'));
        query.where(p);
        TypedQuery typedQuery = em.createQuery(query);        
        return typedQuery.getResultList();
        
        
        
    }
}
